<?php
class installController
{
	public function index()
	{
        if( $_GET['a'] == 'ajax' )
        {
            $this->ajax();
        }

		Kernel::layer('js')->addFile('js/install.js');
        
		Kernel::layer('view')->set('windows', is_dir('C:/'));
		Kernel::layer('view')->unload('install');
		Kernel::dropLayer('view');
	}
	
	public function ajax()
	{	
		if( $_GET['check'] == 'yes' )
		{
			if( file_exists( str_replace('//', '/', $_GET['path'].'/config.lua' ) ) )
			{
				die('y');
			}
			die('n');
		}
		else
		{
			if( !file_exists( str_replace('//', '/', $_GET['path'].'/' ) . 'config.lua' ) )
			{
				die('bp');
				return false;
			}
			
			clearstatcache();
			$cm = substr( sprintf( '%o', fileperms( 'config.php' ) ), -4 );
			
			if( $cm == '0777' or is_dir('C:/') )
			{
				include 'config.php';
				
				$h = fopen( 'config.php', 'w+' );
				fwrite( $h, "<?php\n\$OTSPath = '" . str_replace('//', '/', $_GET['path'].'/' ) . "';\n?>" );
				//fwrite( $h, str_replace( '$OTSPath = \'' . $OTSPath . '\';', '$OTSPath = \'' . str_replace('//', '/', $_GET['path'].'/' ) . '\';', fread($h, filesize('config.php') ) ) );
				fclose( $h );
				
				$ots = new OTSLayer;
				$ots->load();
				
				if( strtolower( $ots->getConfig( 'sql_type' ) ) == 'sqlite' )
				{
					$f = file( 'docs/SQL/schema.sqlite' );
				}
				else
				{
					$f = file( 'docs/SQL/schema.mysql' );
				}
				
				$Query = '';
				foreach( $f as $QueryLine )
				{
					$Query .= $QueryLine;
				}
				
				foreach( explode(';', $Query) as $Q )
				{
					$ots->db->query( $Q ); 
				}
				
				die('y');
                
			}
			
			die('bcm');
		}
	}
}
?>